package com.tma.utils;

import java.util.ArrayList;
import com.tma.model.*;

public class JsonUtils {
	
	/**
	 * 
	 * @param method : create/update/delete
	 * @param value
	 * @return JSON string response for the method
	 */
	public static String buildStringReponseValue(String method, String value)
	{
		StringBuilder retStr = new StringBuilder();
		retStr.append("{");
		retStr.append("method" + ":" + "\"" + method + "\",");
		retStr.append("value" + ":" + "\"" + value + "\"");
		retStr.append("}");
		
		return retStr.toString();
	}
	
	/**
	 * 
	 * @param graph: GraphObject
	 * @return JSON string response for get graph request
	 */
	public static String buildStringReponseGetAGraph(GraphObject graph)
	{
		return graph.getJsonDescription();
	}
	
	/**
	 * 
	 * @param graphs : list of GraphObject
	 * @return JSON string response for get allgraph request
	 */
	public static String buildStringReponseGetAllGraph(ArrayList<GraphObject> graphs)
	{
		StringBuilder retString = new StringBuilder();
		
		if(graphs.isEmpty())
		{
			retString.append("{graphs: []}"); //return jsonStr describes an empty list.
		}
		else
		{			
			retString.append("{ graphs : [");			
			GraphObject graph;
			int i = 0;
			for(i = 0; i < graphs.size() - 1; i++)
			{
				graph = graphs.get(i);
				retString.append("{ name: " + "\"" + graph.getGraphName() + "\"},");
			}
			
			graph = graphs.get(i);
			retString.append("{ name: " + "\"" + graph.getGraphName() + "\"}");
			retString.append("]}");
		}
		
		return retString.toString();
	}
}
